4 // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
6 // The use and distribution terms for this software are contained in the file
7 // named license.txt, which can be found in the root of this distribution.
8 // By using this software in any fashion, you are agreeing to be bound by the
9 // terms of this license.
11 // You must not remove this notice, or any other, from this software.
16 namespace Microsoft
.JScript
{
19 using System
.Reflection
;
20 using System
.Reflection
.Emit
;
21 using System
.Collections
;
23 internal sealed class ObjectLiteral
: AST
{
25 internal AST
[] values
;
27 internal ObjectLiteral(Context context
, ASTList propertyList
)
29 int n
= propertyList
.count
;
30 this.keys
= new AST
[n
];
31 this.values
= new AST
[n
];
32 for (int i
= 0; i
< n
; i
++){
33 ASTList pair
= (ASTList
)propertyList
[i
];
34 this.keys
[i
] = pair
[0];
35 this.values
[i
] = pair
[1];
39 internal override void CheckIfOKToUseInSuperConstructorCall(){
40 for (int i
= 0, n
= this.values
.Length
; i
< n
; i
++)
41 this.values
[i
].CheckIfOKToUseInSuperConstructorCall();
44 internal override Object
Evaluate(){
45 JSObject result
= this.Engine
.GetOriginalObjectConstructor().ConstructObject();
46 for (int i
= 0, n
= this.keys
.Length
; i
< n
; i
++)
47 result
.SetMemberValue(this.keys
[i
].Evaluate().ToString(), this.values
[i
].Evaluate());
51 internal override AST
PartiallyEvaluate(){
52 int n
= this.keys
.Length
;
53 for (int i
= 0; i
< n
; i
++){
54 this.keys
[i
] = this.keys
[i
].PartiallyEvaluate();
55 this.values
[i
] = this.values
[i
].PartiallyEvaluate();
60 internal override void TranslateToIL(ILGenerator il
, Type rtype
){
61 int n
= this.keys
.Length
;
62 this.EmitILToLoadEngine(il
);
63 il
.Emit(OpCodes
.Call
, CompilerGlobals
.getOriginalObjectConstructorMethod
);
64 il
.Emit(OpCodes
.Call
, CompilerGlobals
.constructObjectMethod
);
65 for (int i
= 0; i
< n
; i
++){
67 this.keys
[i
].TranslateToIL(il
, Typeob
.String
);
68 this.values
[i
].TranslateToIL(il
, Typeob
.Object
);
69 il
.Emit(OpCodes
.Call
, CompilerGlobals
.setMemberValue2Method
);
71 Convert
.Emit(this, il
, Typeob
.Object
, rtype
);
74 internal override void TranslateToILInitializer(ILGenerator il
){
75 for (int i
= 0, n
= this.keys
.Length
; i
< n
; i
++){
76 this.keys
[i
].TranslateToILInitializer(il
);
77 this.values
[i
].TranslateToILInitializer(il
);